package com.lbssoft.gpsbd.dao.sql;

import com.lbssoft.gpsbd.common.Utils;
import org.apache.ibatis.jdbc.SQL;

import com.lbssoft.gpsbd.model.Device;
import com.mysql.jdbc.StringUtils;

public class DeviceSql {
	private static final String TABLE_NAME = "lbs_devices";

	public String insert(final Device device) {
		return new SQL(){
			{
				INSERT_INTO(TABLE_NAME);

				// 通过条件 判断是否需要更新该字段
				if (StringUtils.isNullOrEmpty(device.getUuid())) {
					VALUES("uuid","'test-"+Utils.getUUID().substring(5)+"'");
				}else{
					VALUES("uuid","#{uuid}");
				}


				if (!StringUtils.isNullOrEmpty(device.getName())) {
					VALUES("name","#{name}");
				}else{
					VALUES("name","#{imei}");
				}
				if (device.getTypeId()!=0) {
					VALUES("typeId","#{typeId}");
				}
				if (!StringUtils.isNullOrEmpty(device.getImei())) {
					VALUES("imei","#{imei}");
				}

				if (!StringUtils.isNullOrEmpty(device.getSim())) {
					VALUES("sim","#{sim}");
				}
				if (device.getGroupId()!=0) {
					VALUES("groupId","#{groupId}");
				}





			}
		}.toString();
	}


	public String update(final Device device) {
		return new SQL(){
			{
				UPDATE(TABLE_NAME);

				// 通过条件 判断是否需要更新该字段
				if (!StringUtils.isNullOrEmpty(device.getName())) {
					SET("name = #{name}");
				}

				if (!StringUtils.isNullOrEmpty(device.getImei())) {
					SET("imei = #{imei}");
			
				}
				if (device.getTypeId()!=0) {
					SET("typeId = #{typeId}");

				}

				if (!StringUtils.isNullOrEmpty(device.getSim())) {
					SET("sim = #{sim}");

				}
				if(device.getSpeedLimit()==0){
					SET("speedLimit = null");
				}else{
					SET("speedLimit = #{speedLimit}");
				}



				WHERE("id= #{id}");
	
			}
		}.toString();
	}
	
}
